package com.smartEdu.service.impl.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.smartEdu.commons.CMap;
import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.dao.ExamDao;
import com.smartEdu.dao.HomeworkSubmitDao;
import com.smartEdu.dao.StatisticalDao;
import com.smartEdu.exception.JsonException;
import com.smartEdu.exception.ServiceException;
import com.smartEdu.model.CompletedModel;
import com.smartEdu.model.ExamPaperModel;
import com.smartEdu.model.HomeworkRecordModel;
import com.smartEdu.model.HomeworkSingleSubmitModel;
import com.smartEdu.model.HomeworkSubmitModel;
import com.smartEdu.model.RongCloudModel;
import com.smartEdu.service.StatisticalService;
import com.smartEdu.utils.JsonUtil;

@Service(value="statisticalServiceImpl")
public class StatisticalServiceImpl extends BaseServiceImpl implements StatisticalService{
	@Autowired
	@Qualifier("homeworkSubmitDaoImpl")
	private HomeworkSubmitDao homeworkSubmitDao;
	
	@Autowired
	@Qualifier(value="examDaoImpl")
	private ExamDao examDao;
	
	@Autowired
	@Qualifier(value="statisticalDaoImpl")
	private StatisticalDao statisticalDao;
	
	@Override
	public void statisticalHomework(Session sess) {
		// TODO Auto-generated method stub
		CMap map=null;
		int autoScoreSum=0;//自动批改的分数
		int manualScoreSum=0;//手动批改的分数
		int scoreSum=0;//总分
	    List completedlist=new ArrayList<>();
	    Map Answer=new HashMap<>();
	    Map answerlist=new HashMap<>();
	    Map autoScore=new HashMap<>();
	    Map manualScore=new HashMap<>();
		HomeworkRecordModel homeworkRecordModel=new HomeworkRecordModel();
	
		try{
			map=SetToMap(sess);
			List<ExamPaperModel> examPaperModels=null;
			List<HomeworkSubmitModel> homeworkSubitModelmodel=null;
			HashMap<String, Object> map2=new HashMap<>();
			map2.put("pk", map.gets("homeworkpk"));
			map2.put("sid",map.gets("id"));
			homeworkSubitModelmodel=homeworkSubmitDao.getHomeworkCompletedInfo(map2);
			examPaperModels=examDao.getExamPaperByPK(map.gets("exampk").toString());
			String homeCompleted=JsonUtil.toJson(homeworkSubitModelmodel.get(0).getCompleted());
		    Object list=examPaperModels.get(0).getScoreList();
		
		    String scoreList=examPaperModels.get(0).getScoreList().toString();
			List<CompletedModel> homeCompleted2=JsonUtil.toListObject(homeCompleted, CompletedModel.class);
			List<CompletedModel> homeCompleted3=paixv(homeCompleted2);
			scoreList=scoreList.replace("[", "");
		    scoreList=scoreList.replace("]", "");
		    String[] score=scoreList.split(", ");
		    int[] scores=new int[score.length];
		 
		    for(int i=0;i<score.length;i++){
		        scores[i]=Integer.parseInt(score[i]);
		     }
		   
		    int k=0;
		    for(CompletedModel completedModel:homeCompleted3){
		    	completedlist.add(completedModel.getQid());
		    	Answer.put(completedModel.getQid(),completedModel.getAnswer());
		    	Map tempMap=new HashMap<>();
		    	tempMap.put("rw",completedModel.getRight_wrong());
		    	tempMap.put("num",1);
		    	answerlist.put(completedModel.getQid(),tempMap);
		    	if(completedModel.getRight_wrong()==0){
		    		autoScoreSum+=scores[k];
		    		autoScore.put(completedModel.getQid(),scores[k]);
		    	}else if(completedModel.getRight_wrong()==2){
		    		manualScoreSum+=scores[k];
		    		manualScore.put(completedModel.getQid(),scores[k]);
		    		
		    	}else if(completedModel.getRight_wrong()==1){
		    		autoScore.put(completedModel.getQid(),0);
		    	}
		    	k++;
		    }
		    int time=(int) (System.currentTimeMillis() / 1000);
		    System.out.println(time);
		    scoreSum=autoScoreSum+manualScoreSum;
		    homeworkRecordModel.setPK(map.gets("homeworkpk"));
		    homeworkRecordModel.setSID(map.gets("id"));
		    homeworkRecordModel.setPending(0);
		    homeworkRecordModel.setCompleted(homeCompleted3.size());	    
		    homeworkRecordModel.setCompletedlist(JsonUtil.toJson(completedlist));
		    homeworkRecordModel.setAnswer(JsonUtil.toJson(Answer));
		    homeworkRecordModel.setAnswerlist(JsonUtil.toJson(answerlist));
		    homeworkRecordModel.setScoreSum(scoreSum);
		    homeworkRecordModel.setAutoScore(JsonUtil.toJson(autoScore));
		    homeworkRecordModel.setAutoScoreSum(autoScoreSum);
		    homeworkRecordModel.setManualScore(JsonUtil.toJson(manualScore));
		    homeworkRecordModel.setManualScoreSum(manualScoreSum);
		    homeworkRecordModel.setStaus(1);
		    homeworkRecordModel.setCompleteTime(time);
		    homeworkRecordModel.sethType(examPaperModels.get(0).getHtype());
		    homeworkRecordModel.setSavetime(time);
            homeworkRecordModel.setTimepoint(15);
            if(manualScore.size()==0){
            	  homeworkRecordModel.setManualScore(null);
            }
		    statisticalDao.addOrUpdateHomeworkRecord(homeworkRecordModel);
		    sess.put(Tag_Tips.RESPCODE, "0");
		}catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("statisticalHomeworkService业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("statisticalHomeworkService业务流程出错11");
		}
		finally {
			map=null;
		}
		
	}
	
	public static List<CompletedModel> paixv(List<CompletedModel> list){
        int swap;
        String str=null;
        for(int i=0;i<list.size()-1;i++){
        	for(int j=i+1;j<list.size();j++){
        		if(list.get(i).getQid()>list.get(j).getQid()){
        			swap=list.get(j).getQid();
        			list.get(j).setQid(list.get(i).getQid());
        			list.get(i).setQid(swap);
        			
        			swap=list.get(j).getQtype();
        			list.get(j).setQtype(list.get(i).getQtype());
        			list.get(i).setQtype(swap);
        			
        			swap=list.get(j).getRight_wrong();
        			list.get(j).setRight_wrong(list.get(i).getRight_wrong());
        			list.get(i).setRight_wrong(swap);
        			
        			str=list.get(j).getAnswer();
        			list.get(j).setAnswer(list.get(i).getAnswer());
        			list.get(i).setAnswer(str);
        			
        		}
        	}
        }
        
	return list;
}

}
